
package com.cib.ajax.rpc.serializer;

import java.math.BigInteger;

import org.json.JSONObject;

import org.jabsorb.serializer.AbstractSerializer;
import org.jabsorb.serializer.MarshallException;
import org.jabsorb.serializer.ObjectMatch;
import org.jabsorb.serializer.SerializerState;
import org.jabsorb.serializer.UnmarshallException;

@SuppressWarnings("unchecked")
public class BigIntegerSerializer extends AbstractSerializer {
	
	private static final long serialVersionUID = 1L;

	
	public Class[] getJSONClasses() {
		return new Class[] { JSONObject.class };
	}

	
	public Class[] getSerializableClasses() {
		return new Class[] { BigInteger.class };
	}

	
	public Object marshall(SerializerState arg0, Object p, Object arg1)
			throws MarshallException {
		
		try {
			BigInteger bigInteger = (BigInteger) arg1;
			JSONObject jso = new JSONObject();
			if (ser.getMarshallClassHints()) {
				jso.put("javaClass", bigInteger.getClass().getName());
			}
			jso.put("value", bigInteger.intValue());
			return jso;
		} catch (Throwable t) {
			throw new MarshallException(t.getMessage());
		}
	}

	
	public ObjectMatch tryUnmarshall(SerializerState arg0, Class arg1,
			Object arg2) throws UnmarshallException {
		return null;
	}

	
	public Object unmarshall(SerializerState arg0, Class arg1, Object arg2)
			throws UnmarshallException {
		
		try {
			JSONObject jso = (JSONObject) arg2;
			int value = jso.getInt("value");
			return BigInteger.valueOf(Integer.valueOf(value).longValue());
		} catch (Throwable t) {
			throw new UnmarshallException(t.getMessage());
		}
	}
}